package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TDeviceInfo;
import com.ruoyi.system.mapper.TDeviceInfoMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TBoxInfoMapper;
import com.ruoyi.system.domain.TBoxInfo;
import com.ruoyi.system.service.ITBoxInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 装箱管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-20
 */
@Service
public class TBoxInfoServiceImpl implements ITBoxInfoService 
{
    @Autowired
    private TBoxInfoMapper tBoxInfoMapper;
    @Autowired
    private TDeviceInfoMapper tDeviceInfoMapper;

    private final ReentrantLock lock = new ReentrantLock();

    private final int code_length = 5;

    private final String prex = "XZD";


    /**
     * 查询装箱管理
     * 
     * @param boxCode 装箱管理主键
     * @return 装箱管理
     */
    @Override
    public TBoxInfo selectTBoxInfoByBoxCode(String boxCode)
    {
        return tBoxInfoMapper.selectTBoxInfoByBoxCode(boxCode);
    }

    /**
     * 查询装箱管理列表
     * 
     * @param tBoxInfo 装箱管理
     * @return 装箱管理
     */
    @Override
    public List<TBoxInfo> selectTBoxInfoList(TBoxInfo tBoxInfo)
    {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            tBoxInfo.setCreateId(SecurityUtils.getUserId());
        }
        return tBoxInfoMapper.selectTBoxInfoList(tBoxInfo);
    }

    /**
     * 新增装箱管理
     * 
     * @param tBoxInfo 装箱管理
     * @param login 0未登录 1已登录
     * @return 结果
     */
    @Override
    public TBoxInfo insertTBoxInfo(TBoxInfo tBoxInfo,int login)
    {

        lock.lock();
        try {
            if (tBoxInfo==null){
                tBoxInfo = new TBoxInfo();
            }
            int i = 1;
            TBoxInfo boxInfo = tBoxInfoMapper.selectTBoxInfoDesc();
            if (boxInfo!=null){
               i = Integer.parseInt(boxInfo.getBoxCode().substring(boxInfo.getBoxCode().length()-code_length))+1;
            }
            String timestamp = new SimpleDateFormat("yyMMdd").format(new Date());
            String formatted = String.format("%0" + code_length + "d", i);
            tBoxInfo.setBoxCode(timestamp +prex+ formatted);

            tBoxInfo.setBoxState("0");
            tBoxInfo.setCkState("0");
            tBoxInfo.setBoxTime(DateUtils.getNowDate());
            tBoxInfo.setCreateTime(DateUtils.getNowDate());
            tBoxInfo.setExtend1("0");
            //登录
            if(login==1){
                tBoxInfo.setCreateId(SecurityUtils.getUserId());
                tBoxInfo.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
            }
            tBoxInfoMapper.insertTBoxInfo(tBoxInfo);
            return tBoxInfo;
        }finally {
            lock.unlock();
        }
    }

    /**
     * 修改装箱管理
     * 
     * @param tBoxInfo 装箱管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTBoxInfo(TBoxInfo tBoxInfo)
    {
        TBoxInfo old = selectTBoxInfoByBoxCode(tBoxInfo.getBoxCode());
        if(!old.getCkState().equals(tBoxInfo.getCkState()) && tBoxInfo.getCkState().equals("1")){
            //箱子出库，里面的设备全部标记为出库
            tBoxInfo.setCkTime(DateUtils.getNowDate());
            TDeviceInfo tDeviceInfo = new TDeviceInfo();
            tDeviceInfo.setCkState("1");
            tDeviceInfo.setCkTime(tBoxInfo.getCkTime());
            tDeviceInfo.setBoxCode(tBoxInfo.getBoxCode());
            tDeviceInfo.setCustomerInfo(tBoxInfo.getCustomerInfo());
            tDeviceInfo.setCustomerAddress(tBoxInfo.getCustomerAddress());
            tDeviceInfo.setCustomerName(tBoxInfo.getCustomerName());
            tDeviceInfo.setCustomerPhone(tBoxInfo.getCustomerPhone());
            tDeviceInfo.setCustomerRemark(tBoxInfo.getCustomerRemark());
            tDeviceInfoMapper.updateTDeviceByBoxCode(tDeviceInfo);
        }
        tBoxInfo.setUpdateTime(DateUtils.getNowDate());

        return tBoxInfoMapper.updateTBoxInfo(tBoxInfo);
    }

    /**
     * 批量删除装箱管理
     * 
     * @param boxCodes 需要删除的装箱管理主键
     * @return 结果
     */
    @Override
    public int deleteTBoxInfoByBoxCodes(String[] boxCodes)
    {
        return tBoxInfoMapper.deleteTBoxInfoByBoxCodes(boxCodes);
    }

    /**
     * 批量封箱
     *
     * @param boxCodes 需要删除的装箱管理主键集合
     * @return 结果
     */
    @Override
    public int fxBoxInfoByBoxCodes(String[] boxCodes){
        return tBoxInfoMapper.fxBoxInfoByBoxCodes(boxCodes);
    }

    /**
     * 批量出库
     *
     * @param boxCodes 需要删除的装箱管理主键集合
     * @return 结果
     */
    @Override
    @Transactional
    public int ckBoxInfoByBoxCodes(TBoxInfo tBoxInfo,String[] boxCodes){
        tBoxInfoMapper.ckBoxInfoByBoxCodes(tBoxInfo.getCustomerAddress(),tBoxInfo.getCustomerName(),tBoxInfo.getCustomerPhone(),tBoxInfo.getCustomerInfo(),tBoxInfo.getCustomerRemark(),boxCodes);
        tBoxInfoMapper.ckDeviceInfoByBoxCodes(tBoxInfo.getCustomerAddress(),tBoxInfo.getCustomerName(),tBoxInfo.getCustomerPhone(),tBoxInfo.getCustomerInfo(),tBoxInfo.getCustomerRemark(),boxCodes);
        return 1;
    }

    /**
     * 删除装箱管理信息
     * 
     * @param boxCode 装箱管理主键
     * @return 结果
     */
    @Override
    public int deleteTBoxInfoByBoxCode(String boxCode)
    {
        return tBoxInfoMapper.deleteTBoxInfoByBoxCode(boxCode);
    }


}
